package com.gkd.controller;


import com.gkd.dao.EmployeeDao;
import com.gkd.dao.LoginUserDao;
import com.gkd.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    public LoginUserDao loginUserDao;

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model,
                        HttpSession session) throws NoSuchAlgorithmException {
//        loginUserDao.LoginUserMap();//flm
//        loginUserDao.sameNameLoginUserMap();//fsnlm
//        for(int i=0;i<loginUserDao.LoginUserMap().size();i++){
//            if(loginUserDao.LoginUserMap().get(i).getName().equals(username)&&loginUserDao.LoginUserMap().get(i).getPassword().equals(password)){
//                session.setAttribute("loginUser",username);
//                return "redirect:/main.html";
//            }
//        }
//        model.addAttribute("msg","account or password error");
//        return "index";
//        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
//            return "dashboard";
//        System.out.println(username+password);
//        String input = "Hello World!";// 要加密的字符串
//        MessageDigest md = MessageDigest.getInstance("MD5");// 创建MessageDigest对象并指定算法为MD5
//        byte[] inputBytes = input.getBytes();// 将字符串转换成字节数组
//        byte[] hashBytes = md.digest(inputBytes);// 对字节数组进行加密
//        String md5Hash = new BigInteger(1, hashBytes).toString(16);// 将加密后的字节数组转换成十六进制字符串
//        System.out.println("md5Hash=>"+md5Hash);//打印十六进制字符串
        if(loginUserDao.flm==null){
            loginUserDao.LoginUserMap();
        }
        System.out.println("flm=>"+loginUserDao.flm);
        Map<Integer, LoginUser> dl = loginUserDao.LoginUserMap();
//        Map<Integer, LoginUser> sameNameLoginUser = loginUserDao.sameNameLoginUserMap(username);
//        System.out.println(sameNameLoginUser);
        System.out.println(dl);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] passwordBytes = password.getBytes();
        byte[] hashBytes = md.digest(passwordBytes);
        String md5password = new BigInteger(1, hashBytes).toString(16);
        for(int i=0 ;i<dl.size() ;i++){
            if(dl.get(i).getName().equals(username)&&dl.get(i).getMdfivepwd().equals(md5password)){
                session.setAttribute("loginUser",username);
                return "redirect:/main.html";
            }
        }
//        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
//            session.setAttribute("loginUser",username);
//            return "redirect:/main.html";
//        }
//        else {
            session.invalidate();
            model.addAttribute("msg","account or password error");
            return "index";
//        }
    }
    //注销
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
}
